package com.fzz.learning_forum.dao;

import com.fzz.learning_forum.domain.Article;
import org.apache.ibatis.annotations.*;
import org.omg.CORBA.INTERNAL;
import org.springframework.stereotype.Component;

import java.awt.geom.AffineTransform;
import java.util.List;

@Mapper
@Component
public interface ArticleMapping {

    //分页查询文章
    @Select("select * from tb_article ORDER BY id DESC")
    public List<Article> selectArticleWithPage();


    //根据id查询自己的文章
    @Select("select * from tb_article where user_id=#{id} ORDER BY id DESC")
    public List<Article> selectArticleWithPageById(@Param("id") Integer id);

    //根据tags模糊查询文章
    @Select("<script> " +
            "select * from tb_article where 1=1" +
            "<if test='tags!=null'> and tags like concat('%',#{tags},'%')</if>" +
            "ORDER BY id DESC"+
            "</script>")
    public List<Article> selectArticleByTags(@Param("tags") String tags);


    //发布动态
    @Insert("insert into tb_article(title,content,tags,created,modified,user_id,article_img,hit,view) values(#{title},#{content}," +
            "#{tags},#{created},#{modified},#{user_id},#{article_img},#{hit},#{view})")
    public Integer publishArticle(Article article);


    //根据id查询动态细节
    @Select("select * from tb_article where id=#{id}")
    public Article selectArticleDetail(@Param("id") Integer id);

    //根据id修改动态
    @Update("update tb_article set title=#{title},content=#{content},tags=#{tags},modified=#{modified} where id=#{id}")
    public Integer editArticle(Article article);

    //根据id删除动态
    @Delete("delete from tb_article where id=#{id}")
    public Integer deleteArticle(@Param("id") Integer id);

    //根据id更新动态的热度值
    @Delete("update tb_article set hit=#{hit} where id=#{id}")
    public Integer updateArticleHit(@Param("id") Integer id, @Param("hit")Integer hit);

    //根据id更新动态的浏览量
    @Delete("update tb_article set view=#{view} where id=#{id}")
    public Integer updateArticleView(@Param("id") Integer id, @Param("view")Integer view);

    //根据热度值排序
    @Select("select * from tb_article where hit!='0' order by hit desc")
    public List<Article> selectArticleByHit();

    //根据浏览量排序
    @Select("select * from tb_article where view!='0' order by view desc")
    public List<Article> selectArticleByView();

    //根据热度值和浏览量获得热门推荐
    @Select("select * from tb_article order by hit+view desc limit 5")
    public List<Article> selectArticleByHitAndView();
}
